<template>
  <div class="pagination">
    <button>上一页</button>
    <button>1</button>
    <button>···</button>

    <button v-for="(a,b) in startAndEnd.end" :key="b" v-if="a>=startAndEnd.start">{{a}}</button>

    <button>···</button>
    <button>{{ totalPage }}</button>
    <button>下一页</button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
    <h1>{{ startAndEnd }}</h1>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["total", "pageSize", "currentPage", "pagerCount"],
  computed: {
    // 总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    // 计算出连续页码数的开始与结束
    startAndEnd() {
      const { currentPage, pagerCount, totalPage } = this;
      // 准备两个空数准备储存
      let start = null,
        end = null;
      start = currentPage - parseInt(pagerCount / 2);
      end = currentPage + parseInt(pagerCount / 2);
      //左边出现的错误情况
      if (start < 1) {
        start = 1;
        if (totalPage > pagerCount) {
          end = pagerCount;
        } else {
          end = totalPage;
        }
      }
      //   右边出现的错误情况
      if (end > totalPage) {
        end = totalPage;
        start = end - pagerCount + 1;
      }
      return { start, end };
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>